// var exec = require('child_process').exec; // Load the javascript ‘child_process’ library to access the server command line


// var http = require('http');
// var server = http.createServer(function (request, response) {
//     response.end('ok');
// });
// var nodes = require('multi-node').listen({
//     port: 8883,
//     nodes: 2
// }, server);
// console.log('Server running at http://127.0.0.1:8883/');

// var app = require('express').createServer()
//   , io = require('socket.io').listen(app);

// app.listen(80);

// app.get('/', function (req, res) {
//   res.sendfile(__dirname + '/index.html');
// });

// io.sockets.on('connection', function (socket) {
//   socket.emit('news', { hello: 'world' });
//   socket.on('my other event', function (data) {
//     console.log(data);
//   });
// });

var port = 8000
,clients = {}
,app = require('express').createServer()
,io = require('socket.io').listen(app);
app.listen(port);

io.log.info('server started on port', port);

var g1 = io
  .of('/g1')
  .on('connection', function (socket) {

    socket.emit('message','hi! you are welcome! g1');
    // 授权
    socket.on('authorize', function (client, callback) {
      if (client.clientIp && !clients[client.clientIp]) {
        clients[client.clientIp] = socket.client = client;
        socket.broadcast.emit('message', client.user + ' connected');

        var userinfo;
        callback(true,userinfo);
        //io.sockets.emit('message', userinfos);
      }else{
        callback(false,null);
      }
    });

    socket.on('disconnect', function () {
      if (!socket.client) return;
      delete clients[socket.client];
      socket.broadcast.emit('message', socket.client.user + ' disconnected');
      //socket.broadcast.emit('message', userinfos);
    });

    socket.on('log', function (msg) {
      io.log.info(msg);
    });

  });

// var g2 = io
//   .of('/g2')
//   .on('connection', function (socket) {
//     socket.emit('connected', { msg: '2.you are welcome!' });
//   });


